package com.example.mydatasingleservice.services;

import android.util.Log;

import androidx.lifecycle.Observer;

import com.example.mydatasingleservice.DataSingle;
import com.example.mydatasingleservice.MApplication;
import com.example.mydatasingleservice.bean.AData;
import com.example.mydatasingleservice.bean.BData;


public class AService extends BaseServiceListenner{
    private final static String TAG = "AService";
    @Override
    protected void initLiveDataObserver() {
        super.initLiveDataObserver();
        final Observer<AData> aDataObserver = new Observer<AData>() {
            @Override
            public void onChanged(AData aData) {
                switch (aData.getaDataState()){
                    case NO:
                        Log.d(TAG, " A任务完成");
                        break;
                    case INIT:
                        Log.d(TAG, "INIT: ");
                        if(!DataSingle.getInstance().checkBData()) {
                            DataSingle.getInstance().changeBdata(BData.BDataState.INIT);
                            MApplication.startBservice();
                        }else {
                            Log.d(TAG, "onChanged: B正在执行任务");
                        }
                        break;
                    case RUNNING:
                        Log.d(TAG, "RUNNING: ");
                        DataSingle.getInstance().changeAdata(AData.ADataState.FINISH);
                        break;
                    case FINISH:
                        Log.d(TAG, "FINISH: ");
                        DataSingle.getInstance().changeAdata(AData.ADataState.NO);
                        break;
                }
            }
        };
        DataSingle.getInstance().getAmutableLiveData().observe(this,aDataObserver);

    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        DataSingle.getInstance().getAmutableLiveData().removeObservers(this);
    }
}
